<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.carCode"
          placeholder="车牌号码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="司机名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          @change="timeChange"
          clearable
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          type="warning"
          v-if="isAuth('car:white:record:export')"
          @click="handleExport()"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="url"
        header-align="center"
        align="center"
        label="通行车辆照片"
      >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.url"
            :src="getArr(scope.row.url)[0]"
            :preview-src-list="getArr(scope.row.url)"
            style="width: 64px; height: 36px;"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="deviceName"
        header-align="center"
        align="center"
        label="设备名称"
      >
      </el-table-column>

      <el-table-column
        prop="carCode"
        header-align="center"
        align="center"
        label="通行车辆车牌号"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="通行时间"
      >
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageParams.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageParams.limit"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      gridData: [],
      projectList: [],
      dataForm: {
        projectId: "",
        endDate: new Date(parseInt(+new Date()))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        startDate: new Date(parseInt(+new Date() - 3600 * 1000 * 24))
          .toLocaleString()
          .replace(/[/]+/g, "-")
      },
      dataList: [],
      pageParams: {
        page: 1,
        limit: 10
      },
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      trackVisible: false,
      infoVisible: false,
      statusList: [
        {
          label: "异常",
          value: 0
        },
        {
          label: "合格",
          value: 1
        }
      ],

      dateRange: [
        new Date(parseInt(+new Date() - 3600 * 1000 * 24))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-")
      ],
      dateRangeAll: [
        new Date(parseInt(+new Date() - 3600 * 1000 * 24))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-")
      ]
    };
  },
  activated() {
    this.getDataList();
  },
  mounted() {},
  methods: {
    // 处理数组
    getArr(e) {
      return e.split(",");
    },
    timeChange(e) {
      if (e) {
        this.dataForm.startDate = e[0];
        this.dataForm.endDate = e[1];
      } else {
        this.dataForm.startDate = null;
        this.dataForm.endDate = null;
      }
    },
    // 处理数组
    getArr(e) {
      return e.split(",");
    },
    // 导出
    handleExport() {
      let ids;
      if (this.dataListSelections.length === 0) {
        ids = this.dataList.map(item => {
          return item.id;
        });
      } else {
        ids = this.dataListSelections.map(item => {
          return item.id;
        });
      }
      if (ids.length > 0) {
        this.$http({
          url: this.$http.adornUrl(
            `/sys/car/white/record/handleExport?ids=${ids}`
          ),
          method: "post",
          responseType: "blob"
        }).then(({ data }) => {
          console.log(data, "data");
          const blob = new Blob([data], { type: "application/vnd.ms-excel" });
          let a = document.createElement("a");
          a.download = "非运渣车通行记录.xlsx";
          a.href = URL.createObjectURL(blob);
          document.getElementsByTagName("body")[0].appendChild(a);
          a.click();
          a.parentNode.removeChild(a);
        });
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = { ...this.pageParams, ...this.dataForm };
      this.$http({
        url: this.$http.adornUrl("/sys/car/white/record/page"),
        method: "get",
        params: this.$http.adornParams(params)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    sizeChangeHandle(val) {
      this.pageParams.limit = val;
      this.pageParams.page = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageParams.page = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    }
  }
  // 每页数
};
</script>
